
var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
Page({
  data: {
    // 判断手机机型
    isIphoneX: 0,
    inputModal:false,
    textValue:"请输入您想输入的内容",
    info:{},
    page:1,
    size:10,
    count:1,
    allPage:1,
    reviewList:[],
    showTips:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 获取设备信息
    let phone = wx.getStorageSync('isIphoneX')
    if (phone) {
      this.setData({
        isIphoneX: 1
      })
    }
    this.setData({
      info:JSON.parse(options.info)
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.showLoading({
      title: '加载中',
    })
    this.getCommentInfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this;
    if (that.data.page >= that.data.allPage) {
        that.setData({
            showTips: 1
        });
        return false;
    }
    wx.showLoading({
      title: '加载中',
    })
    that.setData({
        page: that.data.page + 1
    });
    that.getCommentInfo();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  showInput(){
    let that=this;
    this.setData({
      inputModal:true
    });
    setTimeout(()=>{
      that.setData({
        textValue:''
      })
    },200)
  },
  onClose() {
    this.setData({ inputModal: false });
  },
  sendEva(){
    let that=this,info=that.data.info,textValue=that.data.textValue,userID=wx.getStorageSync('userID');
    if(this.data.textValue.length==0){
      wx.showToast({
        title: '评价不得为空',
        icon:'none',
        duration:2000
      });
      return;
    }
    util.requestApi(api.setComment,{
      productID : info.productID, //评价的产品ID
      userID : userID,    //评价人
      toUserID : info.userID,  //回复人ID  如果是评价 值为0  评价则传评价人ID
      img : null,     //图片
      content : textValue,  //回复内容
      star : 0,  //好评度
      commentID:info.id,
      parentID:info.id,
      type:1
    }).then((res)=>{
      if(res.code==200){
        that.setData({
          page:1,
          size:8,
          count:1,
          allPage:1,
          reviewList:[],
        });
        wx.showLoading({
          title: '加载中',
        });
        that.getCommentInfo();
      }else{
        wx.showToast({
          title: '评价失败',
          icon:'none',
          duration:2000
        });
      }
    });
  },
  changeInput(e) {
    let value = e.detail.value;
      this.setData({
        textValue: value
      });
  },
  getCommentInfo(){
    let that=this;
    util.requestApi(api.getCommentInfo,{
      commentID:that.data.info.id,
      page:that.data.page,
      size:that.data.size
    }).then((res)=>{
      if(res.code==200){
        that.setData({
          reviewList:that.data.reviewList.concat(res.content.data),
          count:res.content.count,
          allPage: Math.ceil(res.content.count / that.data.size),
          showTips: res.content.data.length < that.data.size ? 1 : 0,
        });
        wx.hideLoading();
      }
    });
  },
})